package com.xish.api.activity.search.query;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.sql.MySQLTemplates;
import com.mysema.query.sql.SQLQuery;
import com.mysema.query.types.OrderSpecifier;
import com.xish.api.activity.search.ActivitySearchCondition;

import java.util.List;

import static com.xish.model.table.QWzmActivitySearch.wzmActivitySearch;

public class ActivityHighlightRuleCommon extends ActivitySearchSqlQuery {

    public static final int limit = 30;

    private boolean limitInQuery = true;

    public ActivityHighlightRuleCommon(ActivitySearchCondition condition, List<Integer> excludeIdList) {
        super(condition, excludeIdList);
    }

    public boolean isLimitInQuery() {
        return limitInQuery;
    }

    public void setLimitInQuery(boolean limitInQuery) {
        this.limitInQuery = limitInQuery;
    }

    @Override
    public SQLQuery getSQLQuery() {
        boolean showWeek = true;

        BooleanBuilder builder = new BooleanBuilder(status());
        builder.and(idNotIn(excludeIdList));

        if (condition.hasPoi()) {
            builder.and(wzmActivitySearch.poiId.eq(condition.getPoiId()));
        }

        if (condition.hasFree()) {
            builder.and(wzmActivitySearch.lowestPrice.eq(0));
        }

        if (condition.hasLimitedDiscouont()) {
            builder.and(wzmActivitySearch.isTimeDiscount.eq(true));
        }

        if (condition.hasKeyword()) {
            showWeek = false;
            builder.and(wzmActivitySearch.keywords.like(condition.getKeyword()));
        } else {
            builder.and(showEndTime());
            builder.and(distance(condition.getGeoLocation()).lt(300D));
        }

        if (condition.hasGenre()) {
            if (condition.getGenreId().equals("67")) {
                showWeek = true;
            } else {
                showWeek = false;
                BooleanBuilder genreExp = new BooleanBuilder();
                String genres[] = condition.getGenreId().split(",");
                for (String genre : genres) {
                    genreExp.or(wzmActivitySearch.genreId.like(genre));
                }
                builder.and(genreExp);
            }
        }

        if (condition.hasKeyword() && condition.hasTag()
                && condition.hasGenre() && condition.hasPoi()) {
            builder.and(showClose());
            builder.and(showFar());
        }

        if (showWeek) {
            builder.and(isShowWeek());
        }

        int pageSize = Integer.valueOf(condition.getPagesize());
        if (pageSize == 0) {
            pageSize = limit;
        }

        OrderSpecifier sort;
        if (condition.getGenreId().equals("67")) {
            sort = sortDayFollowNum();
        } else {
            //todo: 其它的排序方式
            sort = sortDateNew();
        }

        SQLQuery query = new SQLQuery(new MySQLTemplates());
        query.from(wzmActivitySearch);
        query.where(builder);
        query.orderBy(sort);

        if (this.isLimitInQuery()) {
            query.limit(pageSize);
        }

        return query;
    }
}
